/**
 * 简单得增删改查得快速脚手架
 * 
 * 
 * 
 * 
 * 
 */
layui.define(["table", "form", "util"], function (exports) {
  var $ = layui.$,
    table = layui.table
  admin = layui.admin;

  function TABLE() { }

  TABLE.prototype.init = function (config) {
    this.config = config;
    return this;
  }

  TABLE.prototype.render = function () {

    var _config = this.config;

    table.render({
      elem: "#" + _config.TABLEID,
      toolbar: '#toolbarDemo',
      url: _config.PAGEURL, //模拟接口
      contentType: "application/json",
      method: "post",
      cols: _config.cols,
      page: true,
      limit: 30,
      height: "full-100"
    });

    //监听工具条
    table.on("tool(" + _config.TABLEID + ")", function (obj) {
      var data = obj.data;
      if (obj.event === "del") {
        LAYDELLETE(data);
      } else if (obj.event === "edit") {
        LAYOPEN(data.id)
      }
    });

    //表格上部按钮事件事件
    var active = {
      batchdel: LAYDELLETE,
      add: LAYOPEN
    };

    $(document).on("click", ".layui-btn.layuiadmin-btn-useradmin", function () {
      var type = $(this).data("type");
      active[type] ? active[type].call(this) : "";
    });

    //添加编辑函数
    function LAYOPEN(id) {console.debug(id)
      layer.open({
        type: 2,
        title: _config.ADDTITLE,
        content: id ? _config.EDITURL + "?id=" + id : _config.ADDURL,
        maxmin: true,
        area: _config.LAYERAREA,
        btn: ["确定", "取消"],
        yes: function (index, layero) {
          var iframeWindow = window["layui-layer-iframe" + index],
            submitID = _config.FORMSUBMIT,
            submit = layero
              .find("iframe")
              .contents()
              .find("#" + submitID);

          iframeWindow.layui.form.on("submit(" + submitID + ")", function (data) {
            var field = data.field; //获取提交的字段
            if (iframeWindow.ue) {
              field.content = iframeWindow.ue.getContent();
            }console.debug(12112,data.field)
            admin.req({
              url: data.field.id ? _config.EDITURL : _config.ADDURL,
              data: field,
              method: 'post',
              success: function (result) {
                if (result.data) {
                  table.reload(_config.TABLEID); //数据刷新
                  layer.msg("已完成");
                  layer.close(index); //关闭弹层
                } else {
                  layer.msg(result.msg, { icon: 4 });
                }
              }
            })
          });
          submit.trigger("click");
        }
      });
    }
    //删除函数
    function LAYDELLETE(data = null) {
      if (!data) {
        var checkStatus = table.checkStatus(_config.TABLEID),
          checkData = checkStatus.data; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg("请选择数据");
        }

        data = {
          data: $.map(checkData, function (v) {
            return v["id"];
          })
        }
      } else {
        data = {
          data: data.id
        }
      }

      layer.confirm("确定删除吗？", function (index) {
        admin.req({
          url: _config.DELETEURL,
          method: 'post',
          data: data,
          success: function (result) {
            if (result.data) {
              table.reload(_config.TABLEID);
              layer.msg("已删除");
            } else {
              layer.msg(result.msg);
            }
          }
        })
      });
    }

  }
  exports("curdtable", new TABLE());
});